// Springs - Version 1.3

// This script attaches a spring between the 2 layers shown in the popups.
// The layers will continue along at the velocity they had at the
// start of the work area.

//      LAYER                                               PROPERTY                     CHANNEL
//      ------                                              ----------                     --------
// 1: Layer attached to #2 by a spring        doesn't matter               doesn't matter
// 2: Layer attached to #1 by a spring        doesn't matter               doesn't matter

if (time() == start_time) {
	rest_length = 50;					// Rest length of spring in pixels
                                       //  change this to make the spring longer or shorter
	damp = 0.95;								// Damping (0 = infinite friction, 1 = none)

	p1 = value(pop_layer(1), position);
	p2 = value(pop_layer(2), position);

	last_p1 = tmap(time() - step_time, value(pop_layer(1), position));
	last_p2 = tmap(time() - step_time, value(pop_layer(2), position)); 

	v1 = (p1 - last_p1);
	v2 = (p2 - last_p2);
} else {
	delta = p2 - p1;
	n_delta = normalize(delta);

	a = n_delta * (length(delta) - rest_length) * step_time;

	v2 = (v2 - a) * damp;
	v1 = (v1 + a) * damp;

	p1 = p1 + v1;
	p2 = p2 + v2;
}
value(pop_layer(1), position) = p1;
value(pop_layer(2), position) = p2;